//
//  VisitorLoginView.swift
//  微博001
//
//  Created by apple on 15/12/13.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit


//定义协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法
    //用户将要登录
    //optional 是OC中的关键字
    optional func userWillLogin()
    
    //用户注册
    func userWillRegister()
}

class VisitorLoginView: UIView {
    
    
    //声明代理 代理也需要使用weak属性关键字
    //swift 属性默认都是strong 代理同样也是需要使用weak
    weak var visitroViewDelegate: VisitorLoginViewDelegate?
    
    
    
    
    //对外提供设置方可视图信息的方法
    func setUPInfo(tipText: String, imageName: String?) {
        tipLabel.text = tipText
        if let name = imageName {
            //不是首页
            circleView.image = UIImage(named: name)
            //隐藏大房子
            largeIcon.hidden = true
            //将circleView 移动到视图的最顶层
            bringSubviewToFront(circleView)
        }else {
            //首页
            //开始动画
            startAnimation()
            
        }
    }


   private func startAnimation() {
    let anim = CABasicAnimation(keyPath: "transform.rotation")
    //设置动画属性
    anim.repeatCount = MAXFLOAT
    anim.duration = 20.0
    anim.toValue = M_PI * 2
    //当动画完毕或者页失去活跃状态动画不移除图层
    anim.removedOnCompletion = false
    circleView.layer.addAnimation(anim, forKey: nil)
    
        
    }

  
    override init(frame: CGRect) {
        
        //调用super.init
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
    }
    
    
    //约束需要添加到父视图
    //设置所有子视图
   private func setupUI() {
    addSubview(circleView)
    addSubview(backView)
    addSubview(largeIcon)
    
    
    
    addSubview(tipLabel)
    addSubview(registerBtn)
    addSubview(loginBtn)
    
    
    
    //在手动添加约束的时候,需要屏蔽frame布局
    for subview in subviews {
        subview.translatesAutoresizingMaskIntoConstraints = false
    }
    
    addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
    
    //添加圆圈的约束
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
    
    
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
    //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
    
    //按钮的约束
    
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
    //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
    
    
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
    //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
   
    
    
    
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
    
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
    
    
    backgroundColor = UIColor(white: 0.93, alpha: 1)
    
    
    
    registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    
    
    }
    
    //MARK: 按钮的监听事件
    @objc private func registerBtnDidClick() {
        visitroViewDelegate?.userWillRegister()
    }
    
    @objc private func loginBtnDidClick() {
        
        visitroViewDelegate?.userWillLogin?()
    }
    
    
    
    
    
    
    
    
    //懒加载所有的子视图
     private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", fontSize: 15, color: themeColor)
    
    private lazy var loginBtn: UIButton = UIButton(title: "登陆", backImageName: "common_button_white_disable", fontSize: 15, color: UIColor.darkGrayColor())
    

    
    
  
}






























